package com.gitee.dolt.utils.excel.importd;

import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;

/**
 * excel导入结果
 *
 * @author LYH
 */
@Accessors(chain = true)
public class ExcelImportResult implements Serializable {

    @Getter
    @Setter
    private boolean success = true;

    /**
     * 处理条数
     */
    @Getter
    private final AtomicInteger handlerTotal;

    /**
     * 信息
     */
    @Setter
    @Getter
    private String message;

    /**
     * 错误行明细
     */
    @Getter
    private final List<ExcelErrorInfo> excelErrorInfos;

    public ExcelImportResult() {
        this.handlerTotal = new AtomicInteger(0);
        this.excelErrorInfos = new ArrayList<>();
    }

    public ExcelImportResult incrementHandlerTotal() {
        this.handlerTotal.incrementAndGet();
        return this;
    }

    public ExcelImportResult addExcelErrorInfo(ExcelErrorInfo excelErrorInfo) {
        this.excelErrorInfos.add(excelErrorInfo);
        return this;
    }

    public Integer getSuccessCount() {
        int errorRowCount = this.excelErrorInfos.stream().map(e -> e.getRowIndex()).collect(Collectors.toSet()).size();
        return this.handlerTotal.get() - errorRowCount;
    }


}
